package formal.tree.recursion;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 二叉树的最小深度
 *
 * @author DengYuan2
 * @create 2021-01-15 20:29
 */
public class E_111 {
    public static void main(String[] args) {
//        Integer[] a = {3, 9, 20, null, null, 15, 7};
//        Integer[] a = {2,null,3,null,4,null,5,null,6};
        Integer[] a = {0, 2, 4, 1, null, 3, -1, 5, 1, null, 6, null, 8};
        TreeNode treeNode = TreeNode.generateTree(a);
        int res = minDepth4(treeNode);
        System.out.println(res);
    }

    /**
     * 我的写法-太慢
     *
     * @param root
     * @return
     */
    public static int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        if (root.left == null) {
            return 1 + minDepth(root.right);
        }
        if (root.right == null) {
            return 1 + minDepth(root.left);
        }
        return 1 + Math.min(minDepth(root.left), minDepth(root.right));
    }

    /**
     * 大神-和我同样的思路，他的写法简单很多，不过运算都比较慢
     *
     * @param root
     * @return
     */
    public static int minDepth2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = minDepth2(root.left);
        int right = minDepth2(root.right);
        if (left == 0 || right == 0) {
            return 1 + left + right;
        }
        return 1 + Math.min(left, right);
    }

    /**
     * 我的-广度优先遍历-快了许多，但想法过于普通
     *
     * @param root
     * @return
     */
    public static int minDepth3(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int size = 1;
        int depth = 0;
        while (!queue.isEmpty()) {
            depth++;
            int count = size;
            size = 0;
            for (int i = 0; i < count; i++) {
                TreeNode node = queue.poll();
                if (node.left == null && node.right == null) {
                    return depth;
                }
                if (node.left != null && node.right != null) {
                    queue.offer(node.left);
                    queue.offer(node.right);
                    size++;
                }
                if (node.left == null) {
                    queue.offer(node.right);
                }
                if (node.right == null) {
                    queue.offer(node.left);
                }
                size++;
            }
        }
        return depth;
    }

    /**
     * 官方-广度优先遍历
     * 创建新的类，使能够存储节点和该节点的深度
     *
     * @param root
     * @return
     */
    public static int minDepth4(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<QueueNode> queue = new LinkedList<>();
        queue.offer(new QueueNode(root, 1));
        while (!queue.isEmpty()) {
            QueueNode poll = queue.poll();
            TreeNode node = poll.node;
            int depth = poll.depth;
            if (node.left==null&&node.right==null){
                return depth;
            }
            if (node.left!=null){
                queue.offer(new QueueNode(node.left,depth+1));
            }
            if (node.right!=null){
                queue.offer(new QueueNode(node.right,depth+1));
            }
        }
        return 0;
    }


}

class QueueNode {
    TreeNode node;
    int depth;

    public QueueNode(TreeNode node, int depth) {
        this.node = node;
        this.depth = depth;
    }
}